package com.baitai.system.service.impl;

import com.baitai.common.constant.CacheConstants;
import com.baitai.common.core.domain.entity.SysUser;
import com.baitai.common.core.redis.RedisCache;
import com.baitai.common.utils.DateUtils;
import com.baitai.common.utils.StringUtils;
import com.baitai.system.domain.SysConfig;
import com.baitai.system.mapper.*;
import com.baitai.system.service.IIndexService;
import com.baitai.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Service
public class IndexService implements IIndexService {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private UserSignInRecordsMapper signInRecordsMapper;
    @Autowired
    private SysLogininforMapper logininforMapper;
    @Autowired
    private ReceiveTaskMapper receiveTaskMapper;
    @Autowired
    private IssueTaskMapper issueTaskMapper;
    @Autowired
    private ISysConfigService iSysConfigService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public Map<String,String> getUserNumber(Long userId) {
        boolean flag =false;
        if(!"1".equals(isAdmin(userId))){
            flag = true;
        }else {
            userId = null;
        }
        String allUserNumber = userMapper.getUserNumber(userId)+"";
        String allManagerNumber = userMapper.getManagerNumber(userId)+"";
        if(flag){
            allUserNumber = getNumber(allUserNumber,"sys.allUserNumber");
            allManagerNumber = getNumber(allManagerNumber,"sys.allManagerNumber");
        }
        Map<String,String> result = new HashMap<>();
        result.put("allUserNumber",allUserNumber);
        result.put("allManagerNumber",allManagerNumber);

        return result;
    }

    @Override
    public Map<String, String> yesterdayNumber(Long userId) {
        boolean flag =false;
        if(!"1".equals(isAdmin(userId))){
            flag = true;
        }else {
            userId = null;
        }
        String yesterdaySignInNumber = signInRecordsMapper.getYesterdaySignInNumber(DateUtils.yesterdayStart(),DateUtils.yesterdayEnd(),userId)+"";
        String yesterdayLoginInNumber = logininforMapper.getYesterdayLoginInNumber(DateUtils.yesterdayStart(),DateUtils.yesterdayEnd(),userId)+"";
        if(flag){
            yesterdaySignInNumber = getNumber(yesterdaySignInNumber,"sys.yesterdaySignInNumber");
            yesterdayLoginInNumber = getNumber(yesterdayLoginInNumber,"sys.yesterdayLoginInNumber");
        }
        Map<String,String> result = new HashMap<>();
        result.put("yesterdaySignInNumber",yesterdaySignInNumber);
        result.put("yesterdayLoginInNumber",yesterdayLoginInNumber);

        return result;
    }

    @Override
    public Map<String, String> taskNumber(Long userId) {
        boolean flag =false;
        if(!"1".equals(isAdmin(userId))){
            flag = true;
        }else {
            userId = null;
        }
        String waitAuditNumber = receiveTaskMapper.waitAuditNumber(userId)+"";
        String finishNumber = receiveTaskMapper.finishNumber(DateUtils.startOfDay(),DateUtils.endOfDay(),userId)+"";
        if(flag){
            waitAuditNumber = getNumber(waitAuditNumber,"sys.waitAuditNumber");
            finishNumber = getNumber(finishNumber,"sys.finishNumber");
        }
        Map<String,String> result = new HashMap<>();
        result.put("waitAuditNumber",waitAuditNumber);
        result.put("finishNumber",finishNumber);

        return result;
    }

    @Override
    public Map<LocalDate, String> getLineChart(String type,Long userId) {
        if(StringUtils.isEmpty(type)){
            type = "1";
        }
        Date start = DateUtils.startOfDay();
        Date end = DateUtils.endOfDay();
        if("1".equals(type)){
            start = DateUtils.afterDay("7");
        }else if("2".equals(type)){
            start = DateUtils.afterDay("15");
        }else if("3".equals(type)){
            start = DateUtils.afterDay("30");
        }
        // 假设你有一个方法可以生成startDate到endDate之间的所有日期列表
        List<LocalDate> allDates = DateUtils.generateDates(start, end);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 创建一个Map来存储每个日期的计数，默认为0
        Map<LocalDate, String> dailyCounts = new HashMap<>();
        allDates.forEach(date -> dailyCounts.put(date, "0"));
        if("1".equals(isAdmin(userId))){
            userId = null;
        }
        List<Map<String, String>> result = userMapper.getLineChart(start,end,userId);
        // 遍历MyBatis查询结果，更新Map中的计数
        result.forEach(row -> {
            LocalDate date = LocalDate.parse(sdf.format(row.get("date")));
            String count = String.valueOf(row.get("num"));
            dailyCounts.put(date, count+"");
        });


        return dailyCounts;
    }

    @Override
    public Map<String, String> getUserStatistics(Long userId) {
        boolean flag =false;
        if(!"1".equals(isAdmin(userId))){
            flag = true;
        }else {
            userId = null;
        }
        String todayLoginInNumber = logininforMapper.getYesterdayLoginInNumber(DateUtils.startOfDay(),DateUtils.endOfDay(), userId)+"";
        String todayAddUser=userMapper.selectAddNum(DateUtils.startOfDay(),DateUtils.endOfDay(),userId)+"";
        String threeDayAddUser=userMapper.selectAddNum(DateUtils.afterDay("3"),DateUtils.endOfDay(), userId)+"";
        String sevenDayAddUser=userMapper.selectAddNum(DateUtils.afterDay("7"),DateUtils.endOfDay(), userId)+"";
        String sevenDayLoginIn=logininforMapper.getYesterdayLoginInNumber(DateUtils.afterDay("7"),DateUtils.endOfDay(), userId)+"";
//        String monthLoginIn=logininforMapper.getYesterdayLoginInNumber(DateUtils.getStartOfMonth(),DateUtils.endOfDay(), userId)+"";
//        String monthFinishNumber=receiveTaskMapper.finishNumber(DateUtils.getStartOfMonth(),DateUtils.endOfDay(), userId)+"";
//        String monthIssueTaskNumber=issueTaskMapper.issueTaskNumber(DateUtils.getStartOfMonth(),DateUtils.endOfDay())+"";
        if(flag){
            todayLoginInNumber = getNumber(todayLoginInNumber,"sys.todayLoginInNumber");
            todayAddUser = getNumber(todayAddUser,"sys.todayAddUser");
            threeDayAddUser = getNumber(threeDayAddUser,"sys.threeDayAddUser");
            sevenDayAddUser = getNumber(sevenDayAddUser,"sys.sevenDayAddUser");
            sevenDayLoginIn = getNumber(sevenDayLoginIn,"sys.sevenDayLoginIn");
//            monthLoginIn = getNumber(monthLoginIn,"sys.monthLoginIn");
//            monthFinishNumber = getNumber(monthFinishNumber,"sys.monthFinishNumber");
//            monthIssueTaskNumber = getNumber(monthIssueTaskNumber,"sys.waitAuditNumber");
        }
        Map<String,String> result = new HashMap<>();
        result.put("todayLoginInNumber",todayLoginInNumber);
        result.put("todayAddUser",todayAddUser);
        result.put("threeDayAddUser",threeDayAddUser);
        result.put("sevenDayAddUser",sevenDayAddUser);
        result.put("sevenDayLoginIn",sevenDayLoginIn);
//        result.put("monthLoginIn",monthLoginIn);
//        result.put("monthFinishNumber",monthFinishNumber);
//        result.put("monthIssueTaskNumber",monthIssueTaskNumber);
        return result;
    }

    @Override
    public String isAdmin(Long userId) {
        SysUser sysUser = userMapper.selectUserById(userId);
        if(sysUser.isAdmin()){
            return "1";
        }
        return "0";
    }

    @Override
    public String selectConfigByKey(String configKey) {
        return iSysConfigService.selectConfigByKey(configKey);
    }

    @Override
    public SysConfig hasConfig(String configKey) {
        return iSysConfigService.hasConfig(configKey);
    }

    @Override
    public int updateConfigByConfigKey(SysConfig config) {
        return iSysConfigService.updateConfigByConfigKey(config);
    }

    @Override
    public int insertConfig(SysConfig config) {
        return iSysConfigService.insertConfig(config);
    }

    private String getNumber(String number,String configKey){
        String configValue = iSysConfigService.selectConfigByKey(configKey);
        if(StringUtils.isNotEmpty(configValue)){
            BigDecimal value = new BigDecimal(configValue);
            value = value.multiply(new BigDecimal(0.01));
            BigDecimal bigDecimalNumber = new BigDecimal(number);
            return bigDecimalNumber.multiply(value).setScale(0, RoundingMode.CEILING).toString();
        }
        return number;
    }
}
